﻿using Abp.Application.Features;
using Abp.Authorization;
using Abp.Authorization.Roles;
using Abp.Authorization.Users;
using Abp.MultiTenancy;
using Microsoft.EntityFrameworkCore;
using Abp.Zero.EntityFrameworkCore;
using pandx.Mulan.Authorization.Roles;
using pandx.Mulan.Authorization.Users;
using pandx.Mulan.Books;
using pandx.Mulan.Dictionaries;
using pandx.Mulan.Flows;
using pandx.Mulan.Forms;
using pandx.Mulan.MultiTenancy;
using pandx.Mulan.Storage;

namespace pandx.Mulan.EntityFrameworkCore
{
    public class MulanDbContext : AbpZeroDbContext<Tenant, Role, User, MulanDbContext>
    {
      
        
        public DbSet<BinaryObject> BinaryObjects { get; set; }
        
        
        public DbSet<FlowScheme> FlowSchemes { get; set; }
        public DbSet<FlowInstance> FlowInstances { get; set; }
        public DbSet<FlowOperationHistory> FlowOperationHistories { get; set; }
        public DbSet<FlowTransitionHistory> FlowTransitionHistories { get; set; }
        public DbSet<FlowCopy> FlowCopies { get; set; }
        
    
        public DbSet<DictionaryCategory> DictionaryCategories {get;set;}
        public DbSet<Dictionary> Dictionaries {get;set;}
        
        
        public DbSet<Book> Books {get;set;}
        
      
        public DbSet<FormScheme> FormSchemes {get;set;}
        
        public MulanDbContext(DbContextOptions<MulanDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
          
            #region FIX CHANGEABPTABLEPREFIX
            modelBuilder.Entity<TenantFeatureSetting>()
                .HasBaseType<FeatureSetting>().ToTable("TenantFeatures");
            modelBuilder.Entity<EditionFeatureSetting>()
                .HasBaseType<FeatureSetting>().ToTable("EditionFeatures");
            modelBuilder.Entity<UserPermissionSetting>()
                .HasBaseType<PermissionSetting>().ToTable("UserPermissionSettings");
            modelBuilder.Entity<RolePermissionSetting>()
                .HasBaseType<PermissionSetting>().ToTable("RolePermissionSettings");
            #endregion

            modelBuilder.ChangeAbpTablePrefix<Tenant,Role,User>(null);
            
   
            modelBuilder.Entity<User>().Ignore(f => f.OnlyLockedUsers);
            
  
            modelBuilder.Entity<BinaryObject>(b =>
            {
                b.HasIndex(e => new {e.TenantId});
            });
        }
    }
}

